<template>
	<view class="login">
		<view class="img">
			<image src="../../static/images/login.png" mode="scaleToFill"></image>
		</view>
		<view class="btn">
			<!-- <view open-type="getUserInfo" class="top"   @tap="weixinLoin">微信快捷登录
					
				</view> -->
			<button open-type="getUserInfo" class="top" @tap="login">{{loginType}}</button>
			<view class="bott" @tap="phoneLogin">
				手机号登陆
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				loginType: "",
				loginProvider: ""
			}
		},
		onLoad() {
			// #ifdef MP-WEIXIN
			this.loginType = '微信快捷登录'
			this.loginProvider = 'weixin'
			// #endif

			// #ifdef MP-TOUTIAO
			this.loginType = '头条号快捷登录'
			this.loginProvider = 'weixin'
			// #endif

			// #ifdef MP-ALIPAY
			this.loginType = '支付宝快捷登录'
			this.loginProvider = 'weixin'
			// #endif


		},
		methods: {
			login(){
				
				// 如果是支付宝
				// #ifdef MP-ALIPAY
					this.ALiLogin()
				// #endif
				
				
				//如果不是支付宝
				// #ifndef MP-ALIPAY
					this.weixinLoin()
				// #endif
			},
			weixinLoin(e) {
				let _this = this;
				uni.login({
					provider: _this.loginProvider,
					success: function(loginRes) {
						uni.getUserInfo({
							provider: _this.loginProvider,
							success: function(infoRes) {
								uni.setStorage({
									key: "nickName",
									data: infoRes.userInfo.nickName
								})
								uni.setStorage({
									key: "userLogo",
									data: infoRes.userInfo.avatarUrl
								})
								let data = {
									code: loginRes.code
								}
								_this.$http.post('/api/v1.0/appletLogin', data, res => {
									if (res.data.status == '009123') {
										uni.navigateTo({
											url: "../register/register?openId=" + res.data.data
										})
									} else if (res.data.status == '000000') {
										uni.showToast({
											title: "登录成功",
											icon: "none"
										})
										uni.setStorage({
											key: 'userId',
											data: res.data.data.userId,
										})
										uni.setStorage({
											key: 'token',
											data: res.data.data.token,
										})
										uni.setStorage({
											key: 'nickName',
											data: res.data.data.nickName,
										})
										uni.setStorage({
											key: 'buyerAccountId',
											data: res.data.data.buyerAccountId,
										})
										uni.setStorage({
											key: 'userLogo',
											data: res.data.data.logo,
										})
										uni.setStorage({
											key: 'wxOpenid',
											data: res.data.data.wxOpenid,
										})
										uni.setStorage({
											key: 'phone',
											data: res.data.data.mobile,
										})

										uni.switchTab({
											url: '../index/index'
										})
									} else {
										uni.showToast({
											title: res.data.message,
											icon: "none"
										})
									}
								})
							},
							fail: (res) => {
								console.log(res);
								uni.showToast({
									title: '您拒绝了授权，部分功能不能提供',
									icon:"none"
								});
								uni.openSetting({
									 success(res) {
									    console.log(res.authSetting)
									  }
								})
							}
						});

					}
				});

			},
			ALiLogin() {
				let _this = this;
				uni.login({
					provider: _this.loginProvider,
					success: function(loginRes) {
						console.log(loginRes);
						let data = {
							code: loginRes.code
						}
						_this.$http.post('/api/v1.0/appletLogin', data, res => {
							console.log(res)
							if (res.data.status == '009126') {
								uni.navigateTo({
									url: "../register/register?openId=" + res.data.data
								})
							} else if (res.data.status == '000000') {
								uni.showToast({
									title: "登录成功",
									icon: "none"
								})
								uni.setStorage({
									key: 'userId',
									data: res.data.data.userId,
								})
								uni.setStorage({
									key: 'token',
									data: res.data.data.token,
								})
								uni.setStorage({
									key: 'nickName',
									data: res.data.data.nickName,
								})
								uni.setStorage({
									key: 'buyerAccountId',
									data: res.data.data.buyerAccountId,
								})
								uni.setStorage({
									key: 'userLogo',
									data: res.data.data.logo,
								})
								uni.setStorage({
									key: 'wxOpenid',
									data: res.data.data.wxOpenid,
								})
								uni.setStorage({
									key: 'phone',
									data: res.data.data.mobile,
								})

								uni.switchTab({
									url: '../index/index'
								})
							} else {
								uni.showToast({
									title: res.data.message,
									icon: "none"
								})
							}
						})
					},
					fail:(res)=> {
						console.log(res)
					}
				});
			},
			phoneLogin() {
				// uni.navigateTo({
				// 	url: '/pages/login/phoneCodeLogin'
				// });
				uni.navigateTo({
					url: '/pages/login/phoneLogin'
				});
			}
		}
	}
</script>

<style lang="scss">
	.login {
		height: 100vh;
		box-sizing: border-box;
		overflow: hidden;
	}

	// 图片
	.img {
		margin-top: 154px;
		display: flex;
		justify-content: center;

		image {
			width: 131upx;
			height: 131upx;
		}
	}

	// 按钮
	.btn {
		margin-top: 109upx;

		.top {
			width: 616upx;
			height: 88upx;
			background: linear-gradient(360deg, rgba(236, 44, 23, 1) 0%, rgba(249, 98, 58, 1) 100%);
			border-radius: 20upx;
			margin: 0 auto;
			font-size: 32upx;
			font-weight: 500;
			color: rgba(255, 255, 255, 1);
			display: flex;
			align-items: center;
			justify-content: center;
		}

		.bott {
			font-size: 28upx;
			font-weight: 500;
			line-height: 88upx;
			color: rgba(240, 85, 57, 1);
			text-align: center;
		}
	}
</style>
